package jdbc;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import java.util.Properties;

/**
 * select 语句返回 ResultSet
 */
public class ResultSet_ {

    public static void main(String[] args) throws Exception {
        //通过Properties 对象获取配置文件的信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql3.properties"));
        //获取相关值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
//        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        Connection connection = DriverManager.getConnection(url, user, password);

        Statement statement = connection.createStatement();
        //写sql语句
        String sql = "select id, name, sex, borndate from actor";

        ResultSet resultSet = statement.executeQuery(sql);
        //循环取出结果
        while (resultSet.next()) {  // next()让光标下移，如果没有更多行，返回false
            int id = resultSet.getInt(1);    //获取该行的第1列数据
            String name = resultSet.getString(2);
            String sex = resultSet.getString(3);
            Date borndate = resultSet.getDate(4);
            System.out.println(id + "\t" + name + "\t" + sex + "\t" + borndate);
        }
        //关闭连接
        resultSet.close();
        statement.close();
        connection.close();
    }
}
